<template>
  <el-dialog
    v-model="visible"
    :title="getDialogTitle()"
    width="500px"
    destroy-on-close
    :before-close="handleBeforeClose"
  >
    <div class="batch-container">
      <el-alert
        :title="getAlertMessage()"
        :type="operation === 'delete' ? 'error' : 'warning'"
        show-icon
        class="operation-alert"
      ></el-alert>

      <el-card class="items-card">
        <el-checkbox-group v-model="checkedItems" :disabled="true">
          <el-checkbox
            v-for="item in items"
            :key="item.id"
            :label="item"
            class="item-checkbox"
          >
            <div class="item-info">
              <div class="item-name">{{ item.name }} ({{ item.code }})</div>
              <div class="item-meta">
                所属: {{ item.template_name }} | 当前:
                <el-tag
                  :type="item.status === '启用' ? 'success' : 'info'"
                  size="small"
                >
                  {{ item.status }}
                </el-tag>
              </div>
            </div>
          </el-checkbox>
        </el-checkbox-group>
      </el-card>

      <div v-if="operation === 'enable'" class="operation-hint">
        ℹ️ 启用后这些维度将参与风险评分计算
      </div>

      <div v-if="operation === 'disable'" class="operation-hint">
        ℹ️ 禁用后这些维度将不再参与风险评分计算
      </div>

      <div v-if="operation === 'delete'" class="operation-hint danger">
        ⚠️ 删除后将无法恢复，请谨慎操作
      </div>
    </div>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button
        :type="operation === 'delete' ? 'danger' : 'primary'"
        @click="handleConfirm"
      >
        {{ getConfirmButtonText() }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits, watch } from "vue";

// 定义属性和事件
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  operation: {
    type: String,
    default: "", // 'enable', 'disable', 'delete'
    validator: (value) => ["enable", "disable", "delete"].includes(value),
  },
  items: {
    type: Array,
    default: () => [],
  },
});

const emit = defineEmits(["confirm", "update:modelValue"]);

// 响应式数据
const visible = ref(props.modelValue);
const checkedItems = ref([...props.items]);

// 监听显示状态变化
watch(
  () => props.modelValue,
  (val) => {
    visible.value = val;
  }
);

// 监听项目变化
watch(
  () => props.items,
  (items) => {
    checkedItems.value = [...items];
  }
);

// 获取对话框标题
const getDialogTitle = () => {
  const titleMap = {
    enable: "批量启用维度",
    disable: "批量禁用维度",
    delete: "批量删除维度",
  };
  return titleMap[props.operation] || "批量操作";
};

// 获取提示信息
const getAlertMessage = () => {
  const count = props.items.length;
  const messageMap = {
    enable: `即将批量启用 ${count} 个维度，请确认:`,
    disable: `即将批量禁用 ${count} 个维度，请确认:`,
    delete: `即将批量删除 ${count} 个维度，此操作不可恢复，请确认:`,
  };
  return messageMap[props.operation] || "";
};

// 获取确认按钮文本
const getConfirmButtonText = () => {
  const textMap = {
    enable: "确认启用",
    disable: "确认禁用",
    delete: "确认删除",
  };
  return textMap[props.operation] || "确认";
};

// 确认操作
const handleConfirm = () => {
  emit("confirm");
};

// 取消
const handleCancel = () => {
  emit("update:modelValue", false);
};

// 关闭前处理
const handleBeforeClose = () => {
  emit("update:modelValue", false);
  return true;
};
</script>

<style scoped lang="scss">
.batch-container {
  padding: 5px 0;
}

.operation-alert {
  margin-bottom: 16px;
}

.items-card {
  max-height: 300px;
  overflow-y: auto;
  margin-bottom: 16px;
}

.item-checkbox {
  display: block;
  margin-bottom: 10px;
  padding: 8px;
  border-radius: 4px;
  background-color: #f9f9f9;
  transition: background-color 0.2s;

  &:hover {
    background-color: #f0f0f0;
  }
}

.item-info {
  margin-left: 25px;
}

.item-name {
  font-weight: 500;
  margin-bottom: 3px;
}

.item-meta {
  font-size: 13px;
  color: #666;
}

.operation-hint {
  padding: 8px 10px;
  border-radius: 4px;
  font-size: 13px;
  line-height: 1.5;
}

.operation-hint:not(.danger) {
  background-color: #f5f7fa;
  color: #86909c;
}

.danger {
  background-color: #fff1f0;
  color: #f5222d;
}
</style>
